package com.example.adms.businessTrip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adms.businessTrip.dto.*;
import com.example.adms.businessTrip.entity.BusinessTrip;
import com.example.adms.businessTrip.mapper.BusinessTripMapper;
import com.example.adms.businessTrip.service.BusinessTripService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adms.leave.dto.DayInterval;
import com.example.adms.leave.dto.leaveDepart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hyt
 * @since 2023-04-24
 */
@Service
public class BusinessTripServiceImpl extends ServiceImpl<BusinessTripMapper, BusinessTrip> implements BusinessTripService {

    @Autowired
    BusinessTripMapper businessTripMapper;

    @Override
    public int addBusinessTrip(BusinessTripMsg msg) {
        if (ObjectUtils.isNull(msg.getStartTime()) || ObjectUtils.isNull(msg.getEndTime())){
            return 0;
        }
        int result = businessTripMapper.insertBusinessTrip(msg);
        return result;
    }

    @Override
    public Map<String, Object> getBusinessTripList(QueryBusinessTrip queryBusinessTrip) {
        Page<BusinessTrip> page = new Page<>(queryBusinessTrip.getPageNumber(),queryBusinessTrip.getPageSize());
        LambdaQueryWrapper<BusinessTrip> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(queryBusinessTrip.getUserId() != null,BusinessTrip::getUserId,queryBusinessTrip.getUserId());
        queryWrapper.ge(queryBusinessTrip.getStartTime()!=null,BusinessTrip::getStartTime,queryBusinessTrip.getStartTime())
                .le(queryBusinessTrip.getEndTime()!=null,BusinessTrip::getEndTime,queryBusinessTrip.getEndTime());
        queryWrapper.eq(queryBusinessTrip.getPass()!=null,BusinessTrip::getPass,queryBusinessTrip.getPass());
        Page<BusinessTrip> businessTripPage = this.page(page,queryWrapper);
        List<BusinessTrip> leaves = businessTripPage.getRecords();
        Long total = businessTripPage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("list",leaves);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> leaderGetLeaveList(QueryBusinessTrip queryBusinessTrip) {
        Page<BusinessTrip> page = new Page<>(queryBusinessTrip.getPageNumber(), queryBusinessTrip.getPageSize());
        Page<BusinessTrip> leavePage = businessTripMapper.leaderSelectBusinessRecorde(page,queryBusinessTrip);
        Long total = leavePage.getTotal();
        List<BusinessTrip> leaves = leavePage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("list",leaves);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> employeeGetBusinessTripList(QueryBusinessTrip queryBusinessTrip) {
        Page<BusinessTrip> page = new Page<>(queryBusinessTrip.getPageNumber(),queryBusinessTrip.getPageSize());
        LambdaQueryWrapper<BusinessTrip> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(queryBusinessTrip.getUserId() != null,BusinessTrip::getUserId,queryBusinessTrip.getUserId());
        queryWrapper.ge(queryBusinessTrip.getStartTime()!=null,BusinessTrip::getStartTime,queryBusinessTrip.getStartTime())
                .le(queryBusinessTrip.getEndTime()!=null,BusinessTrip::getEndTime,queryBusinessTrip.getEndTime());
        queryWrapper.eq(queryBusinessTrip.getPass()!=null,BusinessTrip::getPass,queryBusinessTrip.getPass());
        Page<BusinessTrip> businessTripPage = this.page(page,queryWrapper);
        List<BusinessTrip> leaves = businessTripPage.getRecords();
        Long total = businessTripPage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("list",leaves);
        map.put("total",total);
        return map;
    }

    @Override
    public boolean examineBusinessTrip(ExamineBusinessTrip examineBusinessTrip) {
        UpdateWrapper<BusinessTrip> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("business_trip_id",examineBusinessTrip.getBusinessTripId())
                .set("pass", examineBusinessTrip.getPass())
                .set("comments",examineBusinessTrip.getComments());
        int result = businessTripMapper.update(null,updateWrapper);
        if (result != 0){
            examineBusinessTrip.setMsg("已审批");
            businessTripMapper.addBusinessTripEvent(examineBusinessTrip);
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public boolean deleteBusinessTrip(int businessTripId) {
        QueryWrapper<BusinessTrip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_trip_id",businessTripId);
        return this.remove(queryWrapper);
    }

    @Override
    public List<Object> businessTripReport(businessTripReport report) {
        //获取所有部门id
        List<leaveDepart> depart = businessTripMapper.selectBusinessTripDepart();
        List<Object> resultList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        String startTime = new DayInterval().getFirstDayOfMonth(year,report.getMonth());
        String endTime = new DayInterval().getLastDayOfMonth(year, report.getMonth());
        for (leaveDepart leaveDepart : depart){
            Map<String,Object> map = new HashMap<>();
            Map<String,Object> param = new HashMap<>();
            param.put("startTime", startTime);
            param.put("endTime", endTime);
            param.put("departId", leaveDepart.getDepartId());
            Integer DepartLeaveNum = businessTripMapper.selectDepartBusinessTripNumByDepartId(param);
            map.put("departId",leaveDepart.getDepartId());
            map.put("departName",leaveDepart.getDepartName());
            map.put("departLeaveNum",DepartLeaveNum);
            resultList.add(map);
        }
        return resultList;
    }


}
